package com.example.springboot0507.service;

import com.example.springboot0507.entity.Dog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Author 王博
 * @DateTime 2025-05-16 08:57
 */
@Slf4j
@Service
public class RedisService {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void set(String key, String value) {
//        redisTemplate.opsForValue().set(key, value);
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void setLocalDateTime(){
        log.info("{}",redisTemplate.getKeySerializer());
        log.info("{}",redisTemplate.getValueSerializer());
        log.info("{}",stringRedisTemplate.getKeySerializer());
        log.info("{}",stringRedisTemplate.getValueSerializer());
        redisTemplate.opsForValue().set("time", LocalDateTime.now().toString());
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        opsForHash.put("time11", "time12", LocalDateTime.now().toString());

        Object o = opsForHash.get("time11", "time12");
        log.info("{}",o);
    }

    public void setDog(){
        redisTemplate.opsForValue().set("dogn",new Dog(1,LocalDateTime.now()));
        redisTemplate.opsForHash().put("dog1n","dog2n",new Dog(2,LocalDateTime.now().minusYears(1)));
    }
}
